package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.listview_library_refrush.PullToRefreshBase;
import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import java.util.ArrayList;

import longrise.phone.com.bjjt_henan.Adapter.MyCollectAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.main.CarNewsActivity;

/**
 * Created by Administrator on 2016/8/26.
 */
public class MyCollectView extends LFView implements AdapterView.OnItemClickListener {

    private Context mContext;

    private View mView;

    private PullToRefreshListView mListView;

    private MyCollectAdapter mAdapter;

    private boolean isLoading = false;// 正在加载

    private ArrayList<EntityBean> beanLists;

    private BaseApplication application;

    private View emptyIV;//无数据时的背景色

    private static final String PAGE = "10";

    private int type;//默认加载我的收藏;1表示加载车主头条

    private String lastId;

    public MyCollectView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter parameter = new FormParameter();
        parameter.setShowtitle(false);
        return parameter;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_mycollect, null);
        if (mView == null) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        if (type == 1) {
            titleTV.setText("车主头条");
        } else {
            titleTV.setText("我的收藏");
        }
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        emptyIV = mView.findViewById(R.id.empty_tv);
        mListView = (PullToRefreshListView) mView.findViewById(R.id.pulltorefresh_listview);
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        // 刚拉时的提示
        mListView.getLoadingLayoutProxy().setPullLabel("加载列表数据...");
        // 正在刷新提示
        mListView.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");
        // 拉倒一定高度的提示
        mListView.getLoadingLayoutProxy().setReleaseLabel("放开以刷新...");
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    lastId = null;
                    loadData();
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    if (beanLists == null || beanLists.size() < 1) {
                        lastId = null;
                        loadData();
                        return;
                    }
                    EntityBean bean = beanLists.get(beanLists.size() - 1);
                    lastId = bean.getString("id");
                    loadData();
                }
            }
        });
        //设置适配器
        mAdapter = new MyCollectAdapter(mContext);
        mAdapter.setData(beanLists);
        mListView.setAdapter(mAdapter);
        //设置点击事件
        setListener(true);

        application = ((MainActivity) mContext).getAppData();
        if (beanLists == null) {
            beanLists = new ArrayList<EntityBean>();
        } else {
            beanLists.clear();
        }
    }

    private void setListener(boolean flag) {
        if (null != mListView) {
            mListView.setOnItemClickListener(flag ? this : null);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position < 1) {
            return;
        }
        EntityBean bean = beanLists.get(position - 1);
        Intent cintent = new Intent(mContext, CarNewsActivity.class);
        cintent.putExtra("databean", bean);
        mContext.startActivity(cintent);
    }

    private void loadData() {
        isLoading = true;
        String username = application.getKckpName();
        String token = application.getToken();
        if (type == 1) {
            ApiImpI.getInstance(mContext.getApplicationContext()).searchMoreNews(username, token, "2", lastId, PAGE, callBack);
        } else {
            ApiImpI.getInstance(mContext.getApplicationContext()).collectNewsList(username, token, lastId, PAGE, callBack);
        }
    }

    private ApiImpI.RequestCallBack callBack = new ApiImpI.RequestCallBack() {
        @Override
        public void onError(String error) {
            if (null != mListView) {// 停止刷新
                mListView.onRefreshComplete();
            }
            DialogUtils.closeDialog();
            isLoading = false;
        }

        @Override
        public void onSuccess(Object result) {
            if (null == mListView) {// 停止刷新
                return;
            }
            mListView.onRefreshComplete();
            DialogUtils.closeDialog();
            EntityBean bean = (EntityBean) result;
            String restate = bean.getString("restate");
            if (TextUtils.equals("1", restate)) {
                EntityBean[] datas = bean.getBeans("data");
                if (null != datas) {
                    if (TextUtils.isEmpty(lastId)) {
                        beanLists.clear();
                    }
                    for (EntityBean data : datas) {
                        beanLists.add(data);
                    }
                } else {
                    String redes = bean.getString("redes");
                    UiUtil.showToast(mContext, redes);
                }
            }
            if (null == beanLists || beanLists.size() <= 0) {
                mListView.setEmptyView(emptyIV);
            } else {
                emptyIV.setVisibility(View.GONE);//null
            }
            if (null != mAdapter) {
                mAdapter.setData(beanLists);
                mAdapter.notifyDataSetChanged();
            }
            isLoading = false;
        }
    };

    @Override
    public void refresh() {
        if (!isLoading) {
            DialogUtils.showDialog(mContext);
            lastId = null;
            loadData();
        }
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListener(false);
        if (null != mListView) {
            mListView.setAdapter(null);
        }
        mListView = null;
        mAdapter = null;
        if (null != beanLists) {
            beanLists.clear();
            beanLists = null;
        }
        mView = null;
    }
}


